package top.lingchen.bootwebadmin.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.lingchen.bootwebadmin.interceptor.LoginInterceptor;
import top.lingchen.bootwebadmin.interceptor.RedisCountInterceptor;

/**
 * @Author 凌宸
 * @create 2021-12-07 下午 9:39
 * @Description
 * @Version 1.0
 */

/**
 * 1. 编写一个拦截器实现 HandlerInterceptor 接口
 * 2. 拦截器注册到容器中，实现（WebMvcConfigurer 的 addInterceptors ） 方法
 * 3. 指定拦截规则 (如果是拦截所有，静态资源也会被拦截)
 */
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {

    @Autowired
    RedisCountInterceptor redisCountInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**") // 所有请求都会被拦截，包括静态资源
                .excludePathPatterns("/", "/login", "/css/**", "/fonts/**", "/images/**", "/js/**"); // 放行哪些请求

        registry.addInterceptor(redisCountInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/", "/login", "/css/**", "/fonts/**", "/images/**", "/js/**");
    }
}
